package by.grsu.shedule.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;
import by.grsu.shedule.R;
import by.grsu.shedule.api.SheduleAPI;
import by.grsu.shedule.constants.ApplicationConstants;
import by.grsu.shedule.fragment.DateFragment;
import by.grsu.shedule.model.DayFromDB;
import by.grsu.shedule.util.CalendarHelper;

public class SheduleManager {

	public static final String SHEDULE_MANAGER = "++SHEDULE_MANAGER++";

	private static final String TAG = SheduleManager.class.getSimpleName();

	private static final int DATABASE_VERSION = 2;

	private static final String DATABASE_NAME = "shedule.store.db";

	private DBSheduleHelper dbHelper;

	private SQLiteDatabase dbReader;

	private SQLiteDatabase dbWriter;

	private Context mContext;

	public SheduleManager(final Context context) {
		dbHelper = new DBSheduleHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
		dbReader = dbHelper.getReadableDatabase();
		dbWriter = dbHelper.getWritableDatabase();
		mContext = context;
	}

	public long addNewLesson(String date, String subject, String teacher,
			String auditory, String group, String timeStart, String timeFinish) {
		if (subject.length() < 1
				&& getLesson(date, timeStart, timeFinish).size() > 0) {
			return 0;
		}
		deletePrevious(date, timeStart, timeFinish);
		return dbWriter.insert(
				DBSheduleHelper.SHEDULE_TABLE,
				null,
				getContentValues(date, subject, teacher, auditory, group,
						timeStart, timeFinish));
	}

	private void deletePrevious(String date, String timeStart, String timeFinish) {
		List<DayFromDB> day = getLesson(date, timeStart, timeFinish);
		for (int i = 0; i < day.size(); i++) {
			if (day.get(i).getSubject().length() < 1) {
				dbWriter.delete(DBSheduleHelper.SHEDULE_TABLE,
						DBSheduleHelper.DATA_SELECTION + "? AND "
								+ DBSheduleHelper.TIME_START_SELECTION
								+ "? AND "
								+ DBSheduleHelper.TIME_FINISH_SELECTION + "?",
						new String[] { date, timeStart, timeFinish });
			}
		}
	}

	public void deleteLesson() {
		List<String> dateList = CalendarHelper.getCurrentDateList();
		for (int i = 0; i < dateList.size(); i++) {
			dbWriter.delete(
					DBSheduleHelper.SHEDULE_TABLE,
					DBSheduleHelper.DATA_SELECTION + "? AND "
							+ DBSheduleHelper.CURRENT_COURSE_SELECTION
							+ "? AND "
							+ DBSheduleHelper.CURRENT_GROUP_SELECTION
							+ "? AND " + DBSheduleHelper.LANGUAGE_SELECTION
							+ "?",
					new String[] {

					dateList.get(i),
							String.valueOf(SheduleAPI.getCurrentCourse()),
							String.valueOf(SheduleAPI.getCurrentGroup()),
							getLanguageFromPrefs() });
		}

	}

	private ContentValues getContentValues(String date, String subject,
			String teacher, String auditory, String group, String timeStart,
			String timeFinish) {
		ContentValues values = new ContentValues();
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.DATA_COL_INDEX],
				date);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.SUBJECT_COL_INDEX],
				subject);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.TEACHER_COL_INDEX],
				teacher);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.AUDITORY_COL_INDEX],
				auditory);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.GROUP_COL_INDEX],
				group);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.TIME_START_COL_INDEX],
				timeStart);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.TIME_FINISH_COL_INDEX],
				timeFinish);
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.CURRENT_COURSE_COL_INDEX],
				SheduleAPI.getCurrentCourse());

		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.CURRENT_GROUP_COL_INDEX],
				SheduleAPI.getCurrentGroup());
		values.put(
				DBSheduleHelper.TABLE_COLUMNS[DBSheduleHelper.LANGUAGE_COL_INDEX],
				getLanguageFromPrefs());
		return values;
	}

	private String getLanguageFromPrefs() {
		return PreferenceManager.getDefaultSharedPreferences(mContext)
				.getString(ApplicationConstants.LANGUAGE_LIST_PREFERENCE,
						mContext.getResources().getString(R.string.translit));
	}

	public List<DayFromDB> getLesson(String date, String timeStart,
			String timeFinish) {
		Cursor cursor = dbReader.query(
				DBSheduleHelper.SHEDULE_TABLE,
				null,
				DBSheduleHelper.DATA_SELECTION + "? AND "
						+ DBSheduleHelper.TIME_START_SELECTION + "? AND "
						+ DBSheduleHelper.TIME_FINISH_SELECTION + "? AND "
						+ DBSheduleHelper.CURRENT_COURSE_SELECTION + "? AND "
						+ DBSheduleHelper.CURRENT_GROUP_SELECTION + "? AND "
						+ DBSheduleHelper.LANGUAGE_SELECTION + "?",
				new String[] { date, timeStart, timeFinish,
						String.valueOf(SheduleAPI.getCurrentCourse()),
						String.valueOf(SheduleAPI.getCurrentGroup()),
						getLanguageFromPrefs() }, null, null, null);
		List<DayFromDB> resultList = new ArrayList<DayFromDB>();
		if (cursor.moveToFirst()) {
			resultList.add(getLesson(cursor));
		}
		cursor.close();
		return resultList;
	}

	public List<DayFromDB> getDayLesson(String date) {
		Cursor cursor = dbReader.query(
				DBSheduleHelper.SHEDULE_TABLE,
				null,
				DBSheduleHelper.DATA_SELECTION + "? AND "
						+ DBSheduleHelper.CURRENT_COURSE_SELECTION + "? AND "
						+ DBSheduleHelper.CURRENT_GROUP_SELECTION + "? AND "
						+ DBSheduleHelper.LANGUAGE_SELECTION + "?",
				new String[] { date,
						String.valueOf(SheduleAPI.getCurrentCourse()),
						String.valueOf(SheduleAPI.getCurrentGroup()),
						getLanguageFromPrefs() }, null, null, null);
		List<DayFromDB> resultList = new ArrayList<DayFromDB>();
		if (cursor.moveToFirst()) {
			resultList.add(getLesson(cursor));
			while (cursor.moveToNext()) {
				resultList.add(getLesson(cursor));
			}
		}
		cursor.close();
		return resultList;
	}

	private DayFromDB getLesson(Cursor cursor) {
		return new DayFromDB(cursor.getString(DBSheduleHelper.DATA_COL_INDEX),
				cursor.getString(DBSheduleHelper.AUDITORY_COL_INDEX),
				cursor.getString(DBSheduleHelper.GROUP_COL_INDEX),
				cursor.getString(DBSheduleHelper.TEACHER_COL_INDEX),
				cursor.getString(DBSheduleHelper.SUBJECT_COL_INDEX),
				cursor.getString(DBSheduleHelper.TIME_START_COL_INDEX),
				cursor.getString(DBSheduleHelper.TIME_FINISH_COL_INDEX));
	}

	public boolean isWeekSaved() {

		Cursor cursor = dbReader.query(
				DBSheduleHelper.SHEDULE_TABLE,
				null,
				DBSheduleHelper.DATA_SELECTION + "? AND "
						+ DBSheduleHelper.CURRENT_COURSE_SELECTION + "? AND "
						+ DBSheduleHelper.CURRENT_GROUP_SELECTION + "? AND "
						+ DBSheduleHelper.LANGUAGE_SELECTION + "?",
				new String[] {
						CalendarHelper.convertDateForSearch(DateFragment
								.getCurrentWeek()),
						String.valueOf(SheduleAPI.getCurrentCourse()),
						String.valueOf(SheduleAPI.getCurrentGroup()),
						getLanguageFromPrefs() }, null, null, null);
		if (cursor.moveToFirst()) {
			return true;
		}

		return false;
	}

}
